package com.yaj.myfactorysupport.controller;

import com.yaj.myfactorysupport.dto.TmWorkerDto;
import com.yaj.myfactorysupport.service.TmWorkerService;
import com.yaj.myfactorysupport.util.RespBean;
import com.yaj.myfactorysupport.vo.TmWorkerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @ClassName LoginController
 * @Description 登录
 * @date 2021/5/2 10:11
 * @Version 1.0
 */
@Api(tags = "LoginController")
@RestController
public class LoginController {
    @Autowired
    private TmWorkerService tmWorkerService;

    @ApiOperation(value = "登录后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody TmWorkerDto tmWorkerDto, HttpServletRequest request){
        RespBean respBean =  tmWorkerService.login(tmWorkerDto.getWorkerId(),request);
        return respBean;
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("/loginworker/info")
    public RespBean getLoginWorkerInfo(Principal principal){
        if(null==principal){
            return null;
        }

        //从token中取得用户ID
      String username = principal.getName();
        TmWorkerVo tmWorkerVo = tmWorkerService.getWorkerInfo(username);

        return RespBean.success(tmWorkerVo);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public  RespBean logout(){
        return RespBean.success("{msg:注销成功}");
    }

}
